import count from '@dfeidao/atom-nodejs/db/count';
import operate from '@dfeidao/atom-nodejs/db/operate';
// import { systemid } from './config';
interface Message {
	_id: string;
	role_name: string;
	role_type_id: string;
	icon: string;
	productid: string;
}

export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {
	const systemid = '12';
	const c = count(action_id, session_id, systemid);
	c.prepare('pub_role', { role_name: msg.role_name, productid: msg.productid });
	const { update, exec } = operate(action_id, session_id, systemid);
	const [t] = await c.exec();
	if (msg.role_name === 'aaa') {
		update('pub_role', { _id: msg._id, productid: msg.productid }, {
			role_type_id: msg.role_type_id,
			icon: msg.icon
		});
		const res1 = await exec();
		if (res1.code === 1) {
			return 'success';
		} else {
			return 'fail';
		}
	} else {

		if (t > 0) {
			return 'other';
		} else {
			update('pub_role', { _id: msg._id, productid: msg.productid }, {
				role_name: msg.role_name,
				role_type_id: msg.role_type_id,
				icon: msg.icon
			});
			const res = await exec();
			if (res.code === 1) {
				return 'success';
			} else {
				return 'fail';
			}
		}
	}


}
